package com.aa.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.aa.Common;
import com.aa.R;
import com.aa.YaloeActivity;
import com.aa.YaloeLauncherActivity;
import com.aa.http.HttpOperation;
import com.aa.setting.BangDingActivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener{

	private String TAG = "LoginActivity";
	private final String mUrl = "http://121.9.64.196:8087/AAproject/api/mobile.php";
	private Button mbtnLogin;
	private Button mbtnRegister;
	private EditText metUserAccount;
	private EditText metUserPassword;
	private TextView mtvFindPassword;
	private ProgressDialog mProgressDialog;
	private Handler mHandler;
	private String mReturErrMSG;
	private String mPhoneNumber;
	private String mPassword;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.longin);
		initView();
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				
				super.handleMessage(msg);
				
				switch (msg.what) {
				case 0:
					Toast.makeText(LoginActivity.this, mReturErrMSG, 3000).show();
					break;
				case 1:
					
						Common.iPassword = mPassword;
						Common.iMyPhoneNumber = mPhoneNumber;
						Common.saveUserInfo(LoginActivity.this);
						Intent _Intent = new Intent();
				        _Intent.setClass(LoginActivity.this, YaloeActivity.class);
				        startActivity(_Intent);
				        finish();
					break;
				case 2:
					Toast.makeText(LoginActivity.this, R.string.http_connect_fail, 3000).show();
					break;

				default:
					break;
				}
			}
			
		};
		
	}
	
	private void initView(){
		mbtnLogin = (Button)findViewById(R.id.btn_login);
		mbtnLogin.setOnClickListener(this);
		mbtnRegister = (Button)findViewById(R.id.btn_register);
		mbtnRegister.setOnClickListener(this);
		metUserAccount = (EditText)findViewById(R.id.et_input_account);
		metUserPassword = (EditText)findViewById(R.id.et_input_password);
		mtvFindPassword = (TextView)findViewById(R.id.tv_forget_pwd);
		mtvFindPassword.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			Login();
			break;
		case R.id.btn_register:
			Intent _Intent = new Intent(LoginActivity.this, RegisterActivity.class);
			startActivity(_Intent);
			break;
		case R.id.tv_forget_pwd:
			break;

		default:
			break;
		}
		
	}
	
	private void Login(){
		String _Account;
		String _Password;
		if (metUserAccount.getText().toString().length() == 0) {
			Toast.makeText(LoginActivity.this, R.string.bangding_7, 3000).show();
			return;
		}
		mPhoneNumber = metUserAccount.getText().toString();
		if (metUserPassword.getText().toString().length() == 0) {
			Toast.makeText(LoginActivity.this, R.string.bangding_8, 3000).show();
			return;
		}
		
		mPassword = metUserPassword.getText().toString();
		
		
		mProgressDialog = ProgressDialog.show(this, getString(R.string.app_progress_title), 
				getString(R.string.app_progress_login), true, false);
		mProgressDialog.setCancelable(true);
		submitLogin(mPhoneNumber, mPassword);
	}
	
	private void submitLogin(String pAccount,String pPassword){		 
			 
				final JSONObject _SendJson = new JSONObject();
				JSONObject _BodyJson = new JSONObject();
				JSONObject _HeadJson = new JSONObject();
				try {
					_HeadJson.put("v", Common.iVersion);
					_HeadJson.put("time", System.currentTimeMillis());
					_HeadJson.put("cmd", 1);
					_HeadJson.put("mac", Common.iMac);
			
					_BodyJson.put("uid", pAccount);
					_BodyJson.put("pwd", pPassword);
													
					_SendJson.put("head", _HeadJson);
					_SendJson.put("body", _BodyJson);
					Log.i(TAG, "_SendJson = " + _SendJson.toString());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				new Thread(new Runnable() {				
					@Override
					public void run() {
						String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
						mProgressDialog.dismiss();
						if (_Result == null || _Result.length() == 0 || _Result.equals("ConnectTimeout")) {
							mHandler.sendEmptyMessage(2);
							return;
						}
						Log.i(TAG, "submitLogin = " + _Result);
						try {
							JSONObject _JsonObject = new JSONObject(_Result); 
							JSONObject _Body = _JsonObject.getJSONObject("body");
						
							if ((_Body.getInt("rcode")) == 1) {												
									Common.iUID = _Body.getString("uid");																																						
									Common.iLogin = 1;					
									Common.iTitile = _Body.getString("title");
									Common.iAutograph = _Body.getString("autograph");
									Common.iServerHeadIconUrl = _Body.getString("head");
									Common.iAgentRank = _Body.getInt("rank");
									String _url1 = _Body.getString("url1");
									
									Common.iBitmapNames.add( _url1);
								
									
									String  _url2 = _Body.getString("url2");								
									 Common.iBitmapNames.add( _url2);
																							
									 String _url3 = _Body.getString("url3");								
									 Common.iBitmapNames.add( _url3);
									
									 String _url4 = _Body.getString("url4");								
									 Common.iBitmapNames.add( _url4);
									
									
									mHandler.sendEmptyMessage(1);
							}else {
								mReturErrMSG = _Body.getString("msg");
								mHandler.sendEmptyMessage(0);
							}
							
						} catch (JSONException e) {
						
							e.printStackTrace();
							
						}
						
					}
				}).start();
		 }
	

}
